//
//  LoginViewController.m
//  CYWFrame
//
//  Created by Macx on 2017/7/21.
//  Copyright © 2017年 Macx. All rights reserved.
//

#import "LoginViewController.h"

#import "TabBarViewController.h"
#import "LoginPhoneViewController.h"
#import "RegisterViewController.h"
#import "LookingPassViewController.h"

#import "NSString+Common.h"
#import <XRCarouselView.h>
#import "AdsServer.h"

#import "LoginServer.h"
#import "PerMessageDetailsViewController.h"

@interface LoginViewController () <UITextFieldDelegate>

@property (nonatomic, strong) AppDelegate *appDelegate;
@property (nonatomic, strong) UITabBarController *mainVC;

@end

@implementation LoginViewController {
    XRCarouselView  *_carouselView;
}

- (void)viewWillAppear:(BOOL)animated {
	if (self.isLogut) {
		//[self.navigationController setNavigationBarHidden:YES animated:animated];
	}
}
- (void)viewWillDisappear:(BOOL)animated {
    //[self.navigationController setNavigationBarHidden:NO animated:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    self.mainVC = [[TabBarViewController alloc] init];
    
    [self buildUI];
    
    [self GETAds];
    
}

#pragma mark ==========构建UI==========
- (void)buildUI {
    
    // 设置导航背景风格颜色
    self.navigationController.navigationBar.barTintColor = UIMainColorRGB;
    // 隐藏导航返回按钮的文字
    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:kWhiteColor}];
    // 设置导航按钮风格颜色
    [[UINavigationBar appearance] setTintColor:kWhiteColor];
	
	self.navigationItem.title = @"登录";
	self.view.backgroundColor = kWhiteColor;
	
	UIButton *returnBtn = [UIButton buttonWithType:UIButtonTypeCustom];
	[returnBtn setImage:IMAGE(@"return") forState:UIControlStateNormal];
	[returnBtn addTarget:self action:@selector(navLeftAction:) forControlEvents:UIControlEventTouchUpInside];
	returnBtn.frame = CGRectMake(0, 0, 44, 44);
	self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:returnBtn];
    
    _carouselView = [[XRCarouselView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    //[self.view addSubview:_carouselView];
    _carouselView.contentMode = UIViewContentModeScaleAspectFill;
    _carouselView.imageArray = @[IMAGE(@"login_bg01")];
    _carouselView.time = 2;

    
//    self.navigationItem.title = @"登录";
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"注册" style:UIBarButtonItemStylePlain target:self action:@selector(registerClick:)];
    
    self.view.backgroundColor = kWhiteColor;
  
    /*
    UILabel *logWay = [[UILabel alloc] init];
    [self.view addSubview:logWay];
    logWay.text = @"选择登录方式";
    logWay.textAlignment = NSTextAlignmentCenter;
    logWay.textColor = kWhiteColor;
    logWay.alpha = 0.8;
    logWay.font = kUHSystemFontWithSize(14);
    CGSize logWaySize = [LZCTools textSizeWithText:logWay.text font:logWay.font];
    logWay.frame = CGRectMake(self.view.centerX-logWaySize.width/2, self.view.height*2/3, logWaySize.width, logWaySize.height);
    
    UIView *line1 = [[UIView alloc] initWithFrame:CGRectMake(20, self.view.height*2/3+logWaySize.height/2, logWay.x-30, 1)];
    [self.view addSubview:line1];
    line1.backgroundColor = UISeparatedColorRGB;
    line1.alpha = 0.8;
    
    UIView *line2 = [[UIView alloc] initWithFrame:CGRectMake(logWay.x+logWay.width+10, self.view.height*2/3+logWaySize.height/2, logWay.x-30, 1)];
    [self.view addSubview:line2];
    line2.backgroundColor = kLightGrayColor;
    line2.alpha = 0.8;
	 
    CGFloat btnSize = (SCREEN_WIDTH-60)/3;
    CGFloat imgY = 0.0;
    NSArray *logWayArr = @[@"微信", @"QQ", @"手机"];
    NSArray *imgArr = @[@"login_wechat", @"login_qq", @"login_phone"];
    
    for (int i = 0; i < logWayArr.count; i++) {
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.view addSubview:btn];
        btn.frame = CGRectMake(20+(10+btnSize)*i, logWay.y+logWay.height+10, btnSize, btnSize);
        btn.tag = 31+i;
        [btn setImage:IMAGE(imgArr[i]) forState:UIControlStateNormal];
        imgY = btn.y;
        [btn addTarget:self action:@selector(logWayClick:) forControlEvents:UIControlEventTouchUpInside];
    
    }
	 */
	
	UIImageView *iconImage = [[UIImageView alloc] initWithFrame:CGRectMake(SW(2)-135/2, Height_NavBar+20, 135, 135)];
	iconImage.image = IMAGE(@"login_logo");
	UILabel *iconNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(SW(2)-(iconImage.width+20)/2, iconImage.y+iconImage.height+10, iconImage.width+20, 50)];
	iconNameLabel.text = @"星河汇";
    iconNameLabel.textAlignment = NSTextAlignmentCenter;
	[self.view addSubview:iconNameLabel];
	[self.view addSubview:iconImage];
	
	UIButton *weChatLogin = [[UIButton alloc] initWithFrame:CGRectMake(SW(10), iconNameLabel.y+iconNameLabel.height+SH(10), SCREEN_WIDTH-SW(5), 56)];
	weChatLogin.layer.borderWidth = 1;
	weChatLogin.layer.borderColor = [UIMainColorRGB CGColor];
	weChatLogin.tag = 31;
	[weChatLogin addTarget:self action:@selector(logWayClick:) forControlEvents:UIControlEventTouchUpInside];
	[self.view addSubview:weChatLogin];
	UIImageView *weChatIcon = [[UIImageView alloc] initWithFrame:CGRectMake(weChatLogin.width/3, 10, 30, 30)];
	weChatIcon.image = IMAGE(@"login_icon_wechat");
	[weChatLogin addSubview:weChatIcon];
	UILabel *weChatLabel = [[UILabel alloc] initWithFrame:CGRectMake(weChatIcon.x+weChatIcon.width+10, 12, 100, 18)];
	weChatLabel.text = @"微信登录";
	weChatLabel.textColor = UIMainColorRGB;
	[weChatLogin addSubview:weChatLabel];
	
	
	
	UIButton *phoneLogin = [[UIButton alloc] initWithFrame:CGRectMake(SW(10), weChatLogin.y+weChatLogin.height+30, SCREEN_WIDTH-SW(5), 56)];
	phoneLogin.layer.borderWidth = 1;
	phoneLogin.layer.borderColor = [UIMainColorRGB CGColor];
	[phoneLogin setBackgroundColor:UIMainColorRGB];
	phoneLogin.tag = 33;
	[phoneLogin addTarget:self action:@selector(logWayClick:) forControlEvents:UIControlEventTouchUpInside];
	[self.view addSubview:phoneLogin];
	UIImageView *phoneIcon = [[UIImageView alloc] initWithFrame:CGRectMake(phoneLogin.width/3, 10, 30, 30)];
	phoneIcon.image = IMAGE(@"login_icon_phone");
	[phoneLogin addSubview:phoneIcon];
	UILabel *phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(phoneIcon.x+phoneIcon.width+10, 15, 100, 18)];
	phoneLabel.text = @"手机登录";
	phoneLabel.textColor = kWhiteColor;
	[phoneLogin addSubview:phoneLabel];
	
	UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(SW(2)-80, phoneLogin.y+phoneLogin.height+30, 107, 18)];
	label1.text = @"还没有账号？";
	UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(label1.x+label1.width, label1.y, 80, 18)];
	label2.text = @"立即注册";
	label2.textColor = UIMainColorRGB;
	[label2 addTarget:self action:@selector(registerClick:)];
	[self.view addSubview:label1];
	[self.view addSubview:label2];
	
    
    UILabel *agreement1 = [[UILabel alloc] init];
    [self.view addSubview:agreement1];
    agreement1.text = @"登录即代表您同意";
    agreement1.textAlignment = NSTextAlignmentCenter;
    agreement1.textColor = [UIColor grayColor];
    agreement1.alpha = 0.8;
    agreement1.font = kUHSystemFontWithSize(12);
    UILabel *agreement2 = [[UILabel alloc] init];
    [self.view addSubview:agreement2];
    agreement2.text = @"星河汇隐私政策";
    agreement2.textAlignment = NSTextAlignmentCenter;
    agreement2.textColor = UIMainColorRGB;
    agreement2.font = kUHSystemFontWithSize(12);
    [agreement2 addTarget:self action:@selector(agreementClick)];
    
    CGSize agreement1Size = [LZCTools textSizeWithText:agreement1.text font:agreement1.font];
    CGSize agreement2Size = [LZCTools textSizeWithText:agreement2.text font:agreement2.font];
    
    agreement1.frame = CGRectMake((SCREEN_WIDTH-(agreement1Size.width+agreement2Size.width))/2, SCREEN_HEIGHT-SW(20)-agreement1Size.height, agreement1Size.width, agreement1Size.height);
    agreement2.frame = CGRectMake(agreement1.x+agreement1Size.width, SCREEN_HEIGHT-SW(20)-agreement1Size.height, agreement2Size.width, agreement2Size.height);
    
    // 下划线
    NSDictionary *attribtDic2 = @{NSUnderlineStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    NSMutableAttributedString *attribtStr2 = [[NSMutableAttributedString alloc]initWithString:agreement2.text attributes:attribtDic2];
    //赋值
    agreement2.attributedText = attribtStr2;
    
    
//    agreement1.hidden = YES;
//    agreement2.hidden = YES;
    
}

#pragma mark ===========响应方法==========
- (void)navLeftAction:(id)sender {
	//    MallViewController *mallVC = [[MallViewController alloc] init];
	//    [self.navigationController popToViewController:mallVC animated:YES];
	AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
	app.window.rootViewController = [[TabBarViewController alloc] init];
}

//跳转到注册
- (void)registerClick:(id)sender {
	NSLog(@"push注册页面");
	[self.navigationController pushViewController:[[RegisterViewController alloc] init] animated:YES];
}

/**
 第三方登录方式
 */
- (void)logWayClick:(UIButton *)sender {
    NSLog(@"登录方式");
    
    if (sender.tag == 31) {
        [self weixinLogin];
    }
    
    else if (sender.tag == 32) {
        [self qqLogin];
    }
    
    else if (sender.tag == 33) {
        [self.navigationController pushViewController:[[LoginPhoneViewController alloc] init] animated:YES];
    }
    
    
}

- (void)qqLogin {

    [LoginServer shareLoginWay:@"qq"];
}

- (void)weixinLogin {
    
    [LoginServer shareLoginWay:@"weixin"];
}
// 获取广告
- (void)GETAds {
    
    [AdsServer GETAds:@{@"board":@"app_bg"} xrView:_carouselView];
    
}
/**
 点击用户协议
 */
- (void)agreementClick {
    NSLog(@"点击用户协议");
    
    PerMessageDetailsViewController *detailVC = [[PerMessageDetailsViewController alloc] init];
    detailVC.type = 1;
    
    [self.navigationController pushViewController:detailVC animated:YES];
}

#pragma mark ===========代理方法==========
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
